package com.yida.wy.vote.outside.service.impl;

import com.yida.common.core.constant.Constants;
import com.yida.common.core.utils.ServletUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.ip.IpUtils;
import com.yida.wy.vote.outside.domain.WyVoteOwnerLoginInfo;
import com.yida.wy.vote.outside.mapper.WyVoteOwnerLoginInfoMapper;
import com.yida.wy.vote.outside.service.IWyVoteOwnerLoginInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业主访问记录Service业务层处理
 *
 * @author lqq
 * @date 2023-03-17
 */
@Service
public class WyVoteOwnerLoginInfoServiceImpl implements IWyVoteOwnerLoginInfoService {

	@Resource
	private WyVoteOwnerLoginInfoMapper voteOwnerLoginInfoMapper;

	/**
	 * 查询业主访问记录
	 *
	 * @param infoId 业主访问记录主键
	 * @return 业主访问记录
	 */
	@Override
	public WyVoteOwnerLoginInfo selectVoteOwnerLoginInfoByInfoId(Long infoId) {
		return voteOwnerLoginInfoMapper.selectVoteOwnerLoginInfoByInfoId(infoId);
	}

	/**
	 * 查询业主访问记录列表
	 *
	 * @param voteOwnerLoginInfo 业主访问记录
	 * @return 业主访问记录
	 */
	@Override
	public List<WyVoteOwnerLoginInfo> selectVoteOwnerLoginInfoList(WyVoteOwnerLoginInfo voteOwnerLoginInfo) {
		return voteOwnerLoginInfoMapper.selectVoteOwnerLoginInfoList(voteOwnerLoginInfo);
	}

	/**
	 * 新增业主访问记录
	 *
	 * @param voteOwnerLoginInfo 业主访问记录
	 * @return 结果
	 */
	@Override
	public int insertVoteOwnerLoginInfo(WyVoteOwnerLoginInfo voteOwnerLoginInfo) {
		return voteOwnerLoginInfoMapper.insertVoteOwnerLoginInfo(voteOwnerLoginInfo);
	}

	/**
	 * 修改业主访问记录
	 *
	 * @param voteOwnerLoginInfo 业主访问记录
	 * @return 结果
	 */
	@Override
	public int updateVoteOwnerLoginInfo(WyVoteOwnerLoginInfo voteOwnerLoginInfo) {
		return voteOwnerLoginInfoMapper.updateVoteOwnerLoginInfo(voteOwnerLoginInfo);
	}

	/**
	 * 批量删除业主访问记录
	 *
	 * @param infoIds 需要删除的业主访问记录主键
	 * @return 结果
	 */
	@Override
	public int deleteVoteOwnerLoginInfoByInfoIds(Long[] infoIds) {
		return voteOwnerLoginInfoMapper.deleteVoteOwnerLoginInfoByInfoIds(infoIds);
	}

	/**
	 * 删除业主访问记录信息
	 *
	 * @param infoId 业主访问记录主键
	 * @return 结果
	 */
	@Override
	public int deleteVoteOwnerLoginInfoByInfoId(Long infoId) {
		return voteOwnerLoginInfoMapper.deleteVoteOwnerLoginInfoByInfoId(infoId);
	}

	/**
	 * 记录业主访问记录
	 *
	 * @param ownerName 用户名
	 * @param status    状态
	 * @param message   消息内容
	 */
	@Override
	public void recordVoteOwnerLoginInfo(String ownerName, String status, String message) {
		WyVoteOwnerLoginInfo loginInfo = new WyVoteOwnerLoginInfo() {{
			setOwnerName(ownerName);
			setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
			setMsg(message);
		}};
		// 日志状态
		if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
			loginInfo.setStatus(Constants.LOGIN_SUCCESS_STATUS);
		} else if (Constants.LOGIN_FAIL.equals(status)) {
			loginInfo.setStatus(Constants.LOGIN_FAIL_STATUS);
		}
		voteOwnerLoginInfoMapper.insertVoteOwnerLoginInfo(loginInfo);
	}
}
